package com.coocaa.aries.jmeter.util;

import com.coocaa.aries.core.tool.random.RandomUtil;
import com.coocaa.aries.jmeter.constant.FileNameConstants;
import com.coocaa.aries.jmeter.entity.ExtTestCaseInfoEntity;
import com.coocaa.aries.jmeter.yml.JmeterProperties;

/**
 * @author : bijiahao
 * @date : 2019/4/1.
 * @description: 路径 工具类
 */
public class PathUtil {
    public static String getJmeterBinDir(JmeterProperties jmeterProperties) {
        StringBuilder jmeterHome = new StringBuilder();
        jmeterHome.append(jmeterProperties.getJmeterHome())
                .append("/")
                .append("bin");
        return jmeterHome.toString();
    }

    /**
     * 获取 测试报告的存放路径-绝对地址
     *
     * @param relativeReportDir
     * @return
     */
    public static String getAbsoluteReportStoreDir(String relativeReportDir, JmeterProperties jmeterProperties) {
        return buildPath(jmeterProperties.getReportStorePath(), relativeReportDir);
    }


    /**
     * 获取 测试用例的存放路径-绝对地址
     *
     * @param relativeJmxPath
     * @return
     */
    public static String getAbsoluteJmxStorePath(String relativeJmxPath, JmeterProperties jmeterProperties) {
        return buildPath(jmeterProperties.getJmxStorePath(), relativeJmxPath);
    }

    /**
     * 获取 测试用例的存放路径-相对地址
     *
     * @return
     */
    public static String getRelativeJmxStorePath() {
        String dir = RandomUtil.getRandomStoreDir();
        return dir + ".jmx";
    }

    /**
     * 获取 测试用例的存放路径-绝对地址
     *
     * @param entity
     * @return
     */
    public static String getAbsoluteJmxStorePath(ExtTestCaseInfoEntity entity, JmeterProperties jmeterProperties) {
        return getAbsoluteJmxStorePath(entity.getTestCaseFilePath(), jmeterProperties);
    }

    /**
     * 获取 测试报告文件存放路径-绝对地址
     *
     * @param entity
     * @return
     */
    public static String getAbsoluteReportStoreDir(ExtTestCaseInfoEntity entity, JmeterProperties jmeterProperties) {
        return getAbsoluteReportStoreDir(entity.getReportFileDir(), jmeterProperties);
    }

    public static String getReportIndexHtmlFilePath(String reportDir) {
        StringBuilder path = new StringBuilder();
        path.append(reportDir);
        if (!reportDir.endsWith("/")) {
            path.append("/");
        }
        path.append("index.html");
        return path.toString();
    }

    public static String getReportPdfFilePath(String reportDir) {
        StringBuilder path = new StringBuilder();
        path.append(reportDir);
        if (!reportDir.endsWith("/")) {
            path.append("/");
        }
        path.append("index.pdf");
        return path.toString();
    }

    public static String getReportZipFilePath(String reportDir) {
        StringBuilder path = new StringBuilder();
        path.append(reportDir);
        if (!reportDir.endsWith("/")) {
            path.append("/");
        }
        path.append(FileNameConstants.ZIP_REPORT_FILE_NAME);
        return path.toString();
    }

    /**
     * 获取 jtl测试结果文件存放路径-绝对地址
     *
     * @param relativeJtlDir
     * @return
     */
    public static String getAbsoluteJtlStorePath(String relativeJtlDir, JmeterProperties jmeterProperties) {
        return buildPath(jmeterProperties.getJtlStorePath(), relativeJtlDir, "result.jtl");
    }

    public static String getAbsoluteJtlStorePath(ExtTestCaseInfoEntity entity, JmeterProperties jmeterProperties) {
        return getAbsoluteJtlStorePath(entity.getReportFileDir(), jmeterProperties);
    }

    /**
     * 获取类路径
     *
     * @return
     */
    public static String getClassPath() {
        String path = PathUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        return path;
    }

    private static String buildPath(String dir, String filePath) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(dir.trim())
                .append("/")
                .append(filePath.trim());
        return stringBuilder.toString();
    }

    private static String buildPath(String dir, String filePath, String name) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(dir.trim())
                .append("/")
                .append(filePath.trim())
                .append("/")
                .append(name)
        ;
        return stringBuilder.toString();
    }
}
